package com.baizhi.service.impl;

import com.baizhi.dao.BookDao;
import com.baizhi.entity.Book;
import com.baizhi.service.BookService;
import com.baizhi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class BookServiceImpl implements BookService {

    @Override
    public List<Book> selectAllBookByPages(String key,String content) {
        List<Book> books=new ArrayList<>();
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books = bookDao.selectAllBookByPages(key,content);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }

    @Override
    public void addBook(Book book) {
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            bookDao.addBook(book);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public Book selectBookById(String bookId) {
        Book book=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            book=bookDao.selectBookById(bookId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return book;
    }

    @Override
    public void updateBook(Book book) {
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            bookDao.updateBook(book);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void deleteBookById(String bookId) {
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            bookDao.deleteBookById(bookId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public List<Book> selectByRecommend() {
        List<Book> books=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books=bookDao.selectByRecommend();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }

    @Override
    public List<Book> selectByHotSale() {
        List<Book> books=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books=bookDao.selectByHotSale();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }

    @Override
    public List<Book> selectByNewBook() {
        List<Book> books=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books=bookDao.selectByNewBook();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }

    @Override
    public List<Book> selectByNewBookHotSale() {
        List<Book> books=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books=bookDao.selectByNewBookHotSale();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }

    @Override
    public List<Book> selectBookByCategoryId(String firstId, String secondId) {
        List<Book> books=null;
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            BookDao bookDao = sqlSession.getMapper(BookDao.class);
            books=bookDao.selectBookByCategoryId(firstId,secondId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return books;
    }
}
